﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Devtm.Generator.ModelStacks
{


    public interface IBoxStack<T> : IDisposable
    {

        T Item { get; }

        IBoxStack<T1> ConvertIn<T1>() where T1 : T;

    }

    public class StackContainer<T> : IBoxStack<T>
    {

        public T Item { get; protected set; }

        internal virtual IStack Stack { get; set; }
        internal virtual Action<object, object> _remove { get; set; }
        internal virtual IStackItem Container { get; set; }

        private StackContainer()
        {

        }

        internal StackContainer(T item, CustomStack<T> st, Action<object, object> remove)
        {
            this.Container = st.container;
            this.Item = item;
            this.Stack = st;
            this._remove = remove;
        }

        public void Dispose()
        {
            _remove(Container, (T)Stack.PopItem());
        }


        public IBoxStack<T1> ConvertIn<T1>()
            where T1 : T
        {
            var result = new StackContainer<T1>();
            result.Container = Container;
            result.Item = (T1)this.Item;            
            result.Stack = Stack;
            result._remove = _remove;
            return result;
        }

    }

  
}
